package com.tamartech.greendb.loader;

import java.util.Date;

import org.apache.log4j.Logger;

import com.tamartech.greendb.dao.ContactDAO;
import com.tamartech.greendb.dao.ElectoralRollDAO;
import com.tamartech.greendb.dao.ElectoralRollEntryDAO;
import com.tamartech.greendb.dao.WardDAO;
import com.tamartech.greendb.data.Contact;
import com.tamartech.greendb.data.ElectoralRoll;
import com.tamartech.greendb.data.ElectoralRollEntry;
import com.tamartech.greendb.data.Ward;

public class VoteTypeLoader
{
    private static Logger log = Logger.getLogger (VoteTypeLoader.class);

    private ElectoralRoll roll;
    
    public VoteTypeLoader()
    {
        
    }
    
    public void begin (String wardName, Date rollDate) throws Exception
    {
        WardDAO wDAO = new WardDAO();
        Ward w = wDAO.get(wardName);
        
        if (w == null)
            throw new Exception ("Ward " + wardName + " not found");
        
        ElectoralRollDAO erDAO = new ElectoralRollDAO();        
        roll = erDAO.getByDate(w, rollDate);
        
        if (roll == null)
            throw new Exception ("Electoral roll for ward: " + wardName + ". date: " + rollDate + " not found");
    }
    
    public void end()
    {
        
    }
    
    public void putVoteType (String refPrefix, 
                               String refSuffix, 
                               String voteType)
    {
        try
        {
            ElectoralRollEntryDAO ereDAO = new ElectoralRollEntryDAO();
            
            ElectoralRollEntry ere = ereDAO.findByReference(roll, refPrefix, refSuffix);
            
            if (ere == null)
                throw new Exception ("Electoral Roll Entry " + refPrefix + " " + refSuffix + " not found");

            ere.setVoteType(voteType);
//            ereDAO.save(ere);
        }
        catch (Exception e)
        {
            log.error("putContact. " + refPrefix + " " + refSuffix, e);
        }
    }
}
